package com.ansion.utils.storage;

import android.text.TextUtils;

import androidx.collection.LruCache;

import com.ansion.constant.BConstant;
import com.ansion.utils.FileUtil;
import com.ansion.utils.ResourceUtils;

import java.io.File;

public class BOTStorage extends BaseStorage {

    public static final String IMAGES = "BOT Images";
    public static final String VIDEOS = "BOT Video";
    public static final String FILES = "BOT Documents";
    public static final String AUDIO = "BOT Audio";
    public static final String OTHER = "BOT Other";
    public static final String ADS = "Ads";
    public static final String LOG = "Log";

    private final LruCache<String, Boolean> fileNameLru;

    private static BOTStorage instance = new BOTStorage();

    public static BOTStorage get() {
        return instance;
    }

    public BOTStorage() {
        fileNameLru = new LruCache<>(100);
    }

    /**
     * 获取media文件夹的位置
     */
    public String getMediaPath() {
        String absolutePath = getRootFileDir(ResourceUtils.getContext()).getAbsolutePath();
        return absolutePath + "/Media/";
    }

    /**
     * 获取.temp文件夹的位置
     */
    public String getTempPath() {
        String absolutePath = getRootFileDir(ResourceUtils.getContext()).getAbsolutePath();
        return absolutePath + "/.temp/";
    }

    /**
     * 获取fragment文件夹的位置
     */
    public String getFragmentPath() {
        String absolutePath = getRootFileDir(ResourceUtils.getContext()).getAbsolutePath();
        return absolutePath + "/fragment/";
    }

    public String getAudioPath() {
        File file = new File(getMediaPath(), AUDIO);
        file.mkdirs();
        return file.getAbsolutePath();
    }

    public String getImagePath() {
        File file = new File(getMediaPath(), IMAGES);
        file.mkdirs();
        return file.getAbsolutePath();
    }

    public String getDocumentPath() {
        File file = new File(getMediaPath(), FILES);
        file.mkdirs();
        return file.getAbsolutePath();
    }

    public String getLogPath() {
        File file = new File(getCacheFileDir(ResourceUtils.getContext()), LOG);
        file.mkdirs();
        return file.getAbsolutePath();
    }

    public String getVideoPath() {
        File file = new File(getMediaPath(), VIDEOS);
        file.mkdirs();
        return file.getAbsolutePath();
    }

    public String getAdsPath() {
        File file = new File(getCacheFileDir(ResourceUtils.getContext()), ADS);
        file.mkdir();
        return file.getAbsolutePath();
    }

    public String getOtherFilePath() {
        File file = new File(getCacheFileDir(ResourceUtils.getContext()), OTHER);
        file.mkdir();
        return file.getAbsolutePath();
    }

    @Deprecated
    public String getMediaResourcePath(String suffix) {
        if (!TextUtils.isEmpty(suffix)) {
//            if (BotFileUtil.hasImage(suffix)) {
//                return getImagePath();
//            }
//
//            if (BotFileUtil.hasVideo(suffix)) {
//                return getVideoPath();
//            }
        }

        return getDocumentPath();
    }

    public String getChatResourcePath(String suffix, int msgType) {
        if (msgType == BConstant.Chat.kChatMsgType_Image ||
                msgType == BConstant.Chat.kChatMsgType_OrigImage ||
                msgType == BConstant.Chat.kChatMsgType_TextImage) {

            File file = new File(getMediaPath(), IMAGES);
            file.mkdirs();
            return file.getAbsolutePath();
        }

        if (msgType == BConstant.Chat.kChatMsgType_ShortVideo ||
                msgType == BConstant.Chat.kChatMsgType_TextVideo) {

            File file = new File(getMediaPath(), VIDEOS);
            file.mkdirs();
            return file.getAbsolutePath();
        }

        if (msgType == BConstant.Chat.kChatMsgType_Audio) {
            File file = new File(getMediaPath(), AUDIO);
            file.mkdirs();
            return file.getAbsolutePath();
        }

        if (msgType == BConstant.Chat.kChatMsgType_File) {
            File file = new File(getMediaPath(), FILES);
            file.mkdirs();
            return file.getAbsolutePath();
        }


        if (!TextUtils.isEmpty(suffix)) {
            if (FileUtil.hasImage(suffix)) {
                File file = new File(getMediaPath(), IMAGES);
                file.mkdirs();
                return file.getAbsolutePath();
            }

            if (FileUtil.hasVideo(suffix)) {
                File file = new File(getMediaPath(), VIDEOS);
                file.mkdirs();
                return file.getAbsolutePath();
            }
        }

        return getDocumentPath();
    }


    public String getImagePath(String uuid, String suffix) {
        File file = new File(getMediaPath(), IMAGES);
        file.mkdirs();
        return file.getAbsolutePath() + File.separator + uuid + suffix;
    }


    private boolean isImage(int messageType) {
        return messageType == BConstant.Chat.kChatMsgType_Image || messageType == BConstant.Chat.kChatMsgType_OrigImage || messageType == BConstant.Chat.kChatMsgType_TextImage;
    }


//    public String getFileSavePath(String downloadUrl, String realFileName, int messageType) {
//        if (isImage(messageType)) {
//            return getImageSavePath(downloadUrl);
//        } else {
//            String suffix = FileUtil.getUrlSuffix(downloadUrl);
//            String chatResourcePath = getChatResourcePath(suffix, messageType);
//
//            if (!TextUtils.isEmpty(realFileName)) {
//                return BotFileUtil.generateFileNameOnly(realFileName, chatResourcePath);
//            } else {
//                String fileName = MD5Util.md5(downloadUrl) + suffix;
//                return chatResourcePath + File.separator + fileName;
//            }
//        }
//    }

    public String getImageSavePath(String downloadUrl) {
        String fileName = FileUtil.getFileNameFromUrl(downloadUrl);
        String suffix = FileUtil.getUrlSuffix(downloadUrl);
        return getChatResourcePath(suffix, BConstant.Chat.kChatMsgType_Image) + File.separator + fileName;
    }

    /**
     * 文件名是否已经存在
     */
    public boolean fileNameExist(String name) {
        return fileNameLru.get(name) != null;
    }

    public void putFileName(String filePath) {
        fileNameLru.put(filePath, true);
    }
}
